<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_inventory_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('4')">隐形采购收货新增</el-button>
      <el-button v-if="permissions.eims_inventory_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('1')">采购收货新增</el-button>
    </el-button-group>
    <el-tabs type="border-card" v-model="tabValue" @tab-click="tabChange">
      <el-tab-pane label="未收货采购订单" name="tab1">
        <el-form inline label-width="100px" :model="dataForm" @keyup.enter.native="searchHandle">
          <el-form-item label="订单号">
            <el-input v-model="dataForm.billId"></el-input>
          </el-form-item>
          <el-form-item label="制单人">
            <el-input v-model="dataForm.createUserName"></el-input>
          </el-form-item>
          <el-form-item label="制单时间">
            <el-row>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.createTimeStart" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-col>
              <el-col :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.createTimeEnd" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="采购类型">
            <el-select v-model="dataForm.ctgId">
              <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="制造商">
            <el-input v-model="dataForm.supplyName" clearable>
              <el-button slot="append" @click="openSupplyDialog">选择</el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="dataForm.viewGoodsName"></el-input>
          </el-form-item>
          <el-form-item label="商品代码">
            <el-input v-model="dataForm.goodsCode"></el-input>
          </el-form-item>
        </el-form>
        <div class="avue-crud">
          <div class="avue-crud">
            <el-table :data="tab1.dataList" border v-loading="tab1.dataListLoading" :max-height="tableHeight">
              <el-table-column label="操作" fixed="right">
                <template slot-scope="scope">
                  <el-button v-if="permissions.eims_inventoryapply_edit" type="text" icon="el-icon-edit"
                    @click="orderReceiveGoods(scope.row.id)">采购收货</el-button>
                </template>
              </el-table-column>
              <el-table-column prop="billId" label="单号" width="160"></el-table-column>
              <el-table-column prop="ctgName" label="商品类别"></el-table-column>
              <el-table-column prop="supplyName" label="制造商"></el-table-column>
              <el-table-column prop="createUserName" label="制单人"></el-table-column>
              <el-table-column prop="createTime" label="制单时间" width="160"></el-table-column>
              <el-table-column prop="auditUserName" label="审核人"></el-table-column>
              <el-table-column prop="auditTime" label="审核时间" width="160"></el-table-column>
              <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
            </el-table>
          </div>
        </div>
        <div class="avue-crud__pagination">
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="tab1.pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="tab1.pageSize" :total="tab1.totalPage" background layout="total, sizes, prev, pager, next">
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="采购收货单" name="tab2">
        <el-form inline label-width="100px" :model="dataForm" @keyup.enter.native="searchHandle">
          <el-form-item label="订单号">
            <el-input v-model="dataForm.billId"></el-input>
          </el-form-item>
          <el-form-item label="关联订单号">
            <el-input v-model="dataForm.associatedBill"></el-input>
          </el-form-item>
          <el-form-item label="运单单号">
            <el-input v-model="dataForm.transportBillId"></el-input>
          </el-form-item>
          <el-form-item label="审核人">
            <el-input v-model="dataForm.auditUserName"></el-input>
          </el-form-item>
          <el-form-item label="审核状态">
            <el-select v-model="dataForm.inveStatus">
              <el-option value="0" label="未审核"></el-option>
              <el-option value="1" label="已审核"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核日期">
            <el-row>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.auditTimeStart" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-col>
              <el-col :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.auditTimeEnd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="收入仓位">
            <el-select v-model="dataForm.inStockId">
              <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="制单人">
            <el-input v-model="dataForm.createUserName"></el-input>
          </el-form-item>
          <el-form-item label="单据日期">
            <el-row>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.createTimeStart" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-col>
              <el-col :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker style="width: 100%;" v-model="dataForm.createTimeEnd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="制造商">
            <el-input v-model="dataForm.supplyName">
              <el-button slot="append" @click="openSupplyDialog">选择</el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="dataForm.viewGoodsName"></el-input>
          </el-form-item>
          <el-form-item label="商品代码">
            <el-input v-model="dataForm.goodsCode"></el-input>
          </el-form-item>
          <el-form-item label="采购类型">
            <el-select v-model="dataForm.ctgId">
              <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="dataForm.memo"></el-input>
          </el-form-item>

          <el-row>
            <el-form-item label="是否显示商品采购数量" label-width="165px">
              <el-checkbox v-model="dataForm.countShowFlag">显示</el-checkbox>
            </el-form-item>
            <el-form-item label="商品品种" prop="brandName" v-if="this.dataForm.countShowFlag">
              <el-input v-model="dataForm.brandName" readonly="readonly">
                <el-button slot="append" @click="getBrandList">选择</el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="部门" v-if="this.dataForm.countShowFlag">
              <el-select v-model="dataForm.deptId">
                <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-row class="bg-row" v-if="this.dataForm.countShowFlag">
            采购商品数量总计：{{ this.tab2.sum}}
          </el-row>
        </el-form>
        <div class="avue-crud">
          <el-table :data="tab2.dataList" border v-loading="tab2.dataListLoading" :max-height="tableHeight2">
            <el-table-column label="操作" width="180" fixed="left">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-collection" @click="checkDetail(scope.row.id)">详情</el-button>
                <el-button v-if="permissions.eims_inventory_edit" :disabled="scope.row.inveStatus == '1'" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.ctgId,scope.row.id)">修改</el-button>
                <el-button v-if="permissions.eims_inventory_del" :disabled="scope.row.inveStatus == '1'" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="billId" label="单据编号" width="160"></el-table-column>
            <el-table-column prop="ctgName" label="采购类型" width="80"></el-table-column>
            <el-table-column prop="inStockName" label="收入仓位"></el-table-column>
            <el-table-column prop="createUserName" label="制单人" width="80"></el-table-column>
            <el-table-column prop="createTime" label="制单日期" width="140"></el-table-column>
            <el-table-column prop="auditUserName" label="审核人" width="80"></el-table-column>
            <el-table-column prop="auditTime" label="审核时间" width="140"></el-table-column>
            <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
          </el-table>
        </div>
        <div class="avue-crud__pagination">
          <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="tab2.pageIndex"
            :page-sizes="[10, 20, 50, 100, 200]" :page-size="tab2.pageSize" :total="tab2.totalPage" background layout="total, sizes, prev, pager, next">
          </el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 弹窗，采购收货 -->
    <receive-goods v-if="receiveGoodsVisible" ref="receiveGoods" @refreshDataList="getDataList"></receive-goods>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <!-- 弹窗，详情信息 -->
    <detail-form v-if="detailVisible" ref="detailForm"></detail-form>
    <!-- 弹窗，查询制造商 -->
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <!-- 弹窗，查询商品品种 -->
    <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { receiveApplyFetchList } from '@/api/eims/purchase/receive-apply'
  import {
    receiveGoodsFetchList,
    receiveGoodsDelObj,
    getGoodsQuantitySum
  } from '@/api/eims/purchase/receive-goods'
  import { getCtgList } from '@/api/eims/ctg'
  import { getStockList } from '@/api/eims/stock'
  import { getDeptInfoByDeptType } from "@/api/admin/dept"

  import ReceiveGoods from './receive.vue'
  import TableForm from './form'
  import DetailForm from './detail'
  import SearchSupply from '@/components/business/SearchSupply'
  import SelectBrand from '../../storage/SearchBrand'
  export default {
    data() {
      return {
        tabValue: 'tab1',
        orderOptionsStart: {},
        orderOptionsEnd: {},
        checkOptionsStart: {},
        checkOptionsEnd: {},
        dataForm: {
          key: ''
        },
        ctgOptions: [],
        stockOptions: [],
        tab1: {
          dataList: [],
          pageIndex: 1,
          pageSize: 200,
          totalPage: 0,
          dataListLoading: false
        },
        tab2: {
          dataList: [],
          pageIndex: 1,
          pageSize: 200,
          totalPage: 0,
          dataListLoading: false,
          sum: ''
        },
        addOrUpdateVisible: false, // 采购收货新增是否显示
        receiveGoodsVisible: false,
        detailVisible: false,
        supplyVisible: false,
        selectBrandVisible: false,
        deptOptions: []
      }
    },
    components: {
      TableForm,
      ReceiveGoods,
      DetailForm,
      SearchSupply,
      SelectBrand
    },
    created() {
      this.getDataList()
      this.getGoodsList()
      this.getStock()
      this.getDeptList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      },
      tableHeight2: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      tabChange(data) {

        if (this.tabValue == 'tab1') {
          this.tab1.pageIndex = 1
        } else {
          this.tab2.pageIndex = 1
        }
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
        this.getDataList()
        // this.orderOptionsStart = {}
        // this.orderOptionsEnd = {}
        // this.checkOptionStart = {}
        // this.checkOptionEnd = {}
        this.orderChangeStart()
        this.orderChangeEnd()
        this.checkChangeEnd()
        this.checkChangeStart()
      },
      //  查询数据
      searchHandle() {
        if (this.tabValue == 'tab1') {
          this.tab1.pageIndex = 1
        } else if (this.tabValue == 'tab2') {
          this.tab2.pageIndex = 1
        }
        this.getDataList()
      },
      //  清空搜索表单
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      // 待收货采购订单
      getDataList() {
        if (this.tabValue == 'tab1') {
          let params1 = {
            current: this.tab1.pageIndex,
            size: this.tab1.pageSize,
            applyStatus: '1'
          }
          let params2 = filterParams(this.dataForm)
          this.tab1.dataListLoading = true
          receiveApplyFetchList(Object.assign(params1, params2)).then(response => {
            if(response.data.code == '0'){
              this.tab1.dataList = response.data.data.records
              this.tab1.totalPage = response.data.data.total
            }else{
              this.$message.error(response.data.msg)
            }
            this.tab1.dataListLoading = false
          })
        } else if (this.tabValue == 'tab2') {
          let params1 = {
            current: this.tab2.pageIndex,
            size: this.tab2.pageSize
          }
          let params2 = filterParams(this.dataForm)
          this.tab2.dataListLoading = true
          receiveGoodsFetchList(Object.assign(params1, params2)).then(response => {
            if(response.data.code == '0'){
              this.tab2.dataList = response.data.data.records
              this.tab2.totalPage = response.data.data.total
            }else{
              this.$message.error(response.data.msg)
            }

            this.tab2.dataListLoading = false
          })
          if (this.dataForm.countShowFlag) {
            getGoodsQuantitySum(params2).then(response => {
              if(response.data.code == '0'){
                this.tab2.sum = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        }
      },
      sizeChangeHandle(val) {
        if (this.tabValue == 'tab1') {
          this.tab1.pageSize = val
          this.tab1.pageIndex = 1
        } else if (this.tabValue == 'tab2') {
          this.tab2.pageSize = val
          this.tab2.pageIndex = 1
        }
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        if (this.tabValue == 'tab1') {
          this.tab1.pageIndex = val
        } else if (this.tabValue == 'tab2') {
          this.tab2.pageIndex = val
        }
        this.getDataList()
      },
      // 打开采购订单弹窗
      orderReceiveGoods(id) {
        this.receiveGoodsVisible = true
        this.$nextTick(() => {
          this.$refs.receiveGoods.init(id)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle(type, id) {
        let data = {
          type: type,
          id: id
        }
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(data)
        })
      },
      //  查看详情
      checkDetail(id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detailForm.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return receiveGoodsDelObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  获取商品列别
      getGoodsList() {
        getCtgList().then(response => {
          if(response.data.code == '0'){
            this.ctgOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  获取仓位列表
      getStock() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  打开选择制造商弹窗
      openSupplyDialog() {
        let data = {
          ctgId: this.dataForm.ctgId
        }
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init(data)
        })
      },
      getSupplyData(data) {
        this.$set(this.dataForm, 'supplyId', data.id)
        this.$set(this.dataForm, 'supplyName', data.name)
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.ctgId) {
          this.$message.warning('请选择商品类别')
          return false
        }
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return false
        }
        let data = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
        this.dataForm.brandCode = data.code
        this.dataForm.name = data.name
        this.dataForm.unitType = data.unitType
      },
      orderChangeStart() {
        let that = this
        this.orderOptionsStart = Object.assign({}, this.orderOptionsStart, {
          disabledDate: (time) => {
            if (this.dataForm.createTimeEnd) {
              return time.getTime() < this.dataForm.createTimeEnd
            } else {
              return false
            }
          }
        })
      },
      orderChangeEnd() {
        this.orderOptionsEnd = Object.assign({}, this.orderOptionsEnd, {
          disabledDate: (time) => {
            if (this.dataForm.createTimeStart) {
              return time.getTime() < this.dataForm.createTimeStart
            } else {
              return false
            }
          }
        })
      },
      checkChangeStart() {
        this.checkOptionsStart = Object.assign({}, this.checkOptionsStart, {
          disabledDate: (time) => {
            if (this.dataForm.auditTimeEnd) {
              return time.getTime() > this.dataForm.auditTimeEnd
            } else {
              return false
            }
          }
        })
      },
      checkChangeEnd() {
        this.checkOptionsEnd = Object.assign({}, this.checkOptionsEnd, {
          disabledDate: (time) => {
            if (this.dataForm.auditTimeStart) {
              return time.getTime() < this.dataForm.auditTimeStart
            } else {
              return false
            }
          }
        })
      },
      getDeptList() {
        getDeptInfoByDeptType(1).then(response => {
          if(response.data.code == '0'){
            this.deptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>
